﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.BLL;
using XiaoGang.Soft.XGWMS.MODEL;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M002UserGroupEdit : FormBase
    {
        # region 变量定义

        enumOperation MOpertion = new enumOperation();
        private const string dataName = "用户分组";

        #endregion

        #region 方法

        public M002UserGroupEdit()
        {
            InitializeComponent();
            txtGROUP_ID.Text = "";
            txtGROUP_NM.Text = "";
            txtMEMO.Text = "";
            txtGROUP_ID.Enabled = false;
        }

        public M002UserGroupEdit(string GroupID, string GroupName, string Memo)
        {
            InitializeComponent();
            txtGROUP_ID.Text = GroupID;
            txtGROUP_NM.Text = GroupName;
            txtMEMO.Text = Memo;
            txtGROUP_ID.Enabled = false;
        }

        /// <summary>
        /// SetControlsIndex
        /// </summary>
        protected void SetControlsIndex()
        {
            int intIndex = 0;
            intIndex++;
            txtGROUP_ID.TabIndex = intIndex;
            intIndex++;
            txtGROUP_NM.TabIndex = intIndex;
            intIndex++;
            txtMEMO.TabIndex = intIndex;
            intIndex++;
            btnAdd.TabIndex = intIndex;
            intIndex++;
            btnClear.TabIndex = intIndex;
            intIndex++;
            btnExit.TabIndex = intIndex;
        }

        /// <summary>
        /// Initial
        /// </summary>
        private void Initial()
        {
            SetControlsIndex();
        }

        /// <summary>
        /// CheckProcessLoop
        /// </summary>
        /// <param name="enumOperation"></param>
        /// <returns></returns>
        protected bool CheckProcessLoop(enumOperation enumOperation)
        {
            switch (enumOperation)
            {                
                case enumOperation.Add:
                    MOpertion = enumOperation.Add;
                    //分组名称
                    if (!CheckProcess(this.txtGROUP_NM.Name)) return false;
                    //备注
                    if (!CheckProcess(this.txtMEMO.Name)) return false;
                    break;
                case enumOperation.Update:
                    MOpertion = enumOperation.Update;
                    //用户分组ID
                    if (!CheckProcess(this.txtGROUP_ID.Name)) return false;
                    //分组名称
                    if (!CheckProcess(this.txtGROUP_NM.Name)) return false;
                    //备注
                    if (!CheckProcess(this.txtMEMO.Name)) return false;
                    break;                
            }
            return true;
        }

        /// <summary>
        /// CheckProcess
        /// </summary>
        /// <param name="strConstrolName"></param>
        /// <returns></returns>
        protected override bool CheckProcess(string strConstrolName)
        {
            string strValue = string.Empty;
            switch (strConstrolName)
            {
                //用户分组ID
                case "txtGROUP_ID":
                    strValue = txtGROUP_ID.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.ItemID, true, null))
                        {
                            txtGROUP_ID.Focus();
                            txtGROUP_ID.SelectAll();
                            return false;
                        }
                    }
                    else if (MOpertion == enumOperation.Add)
                    {
                        break;
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.ItemID, false, null))
                        {
                            txtGROUP_ID.Focus();
                            txtGROUP_ID.SelectAll();
                            return false;
                        }
                    }
                    break;
                //分组名称
                case "txtGROUP_NM":
                    strValue = txtGROUP_NM.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.ItemName, true, null))
                        {
                            txtGROUP_NM.Focus();
                            txtGROUP_NM.SelectAll();
                            return false;
                        }
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.ItemName, false, null))
                        {
                            txtGROUP_NM.Focus();
                            txtGROUP_NM.SelectAll();
                            return false;
                        }
                    }
                    break;
                //备注
                case "txtMEMO":
                    strValue = txtMEMO.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.ItemMEMO, true, null))
                    {
                        this.txtMEMO.Focus();
                        txtMEMO.SelectAll();
                        return false;
                    }
                    break;
            }
            return true;
        }

        private void InitialControl()
        {
            txtGROUP_NM.Text = "";
            txtMEMO.Text = "";
        }

        #endregion

        #region 事件

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M002_USER_GROUP_Load(object sender, EventArgs e)
        {
            Initial();
        }
         
        /// <summary>
        /// btnClear_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            InitialControl();
        }

        /// <summary>
        /// btnExit_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Close();
        }

        /// <summary>
        /// btnAdd_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (txtGROUP_ID.Text.Trim().Length<1 && !this.CheckProcessLoop(enumOperation.Add))
            {
                return;
            }
            if (txtGROUP_ID.Text.Trim().Length>0 && !this.CheckProcessLoop(enumOperation.Update))
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor;

            MOD_M002_USER_GROUP modUserGroup = new MOD_M002_USER_GROUP();
            BLL_M002_USER_GROUP bllUserGroup = new BLL_M002_USER_GROUP();
            ReturnValue returnValue;
            modUserGroup.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID;
            modUserGroup.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD;
            modUserGroup.GROUP_ID = txtGROUP_ID.Text.Trim();
            modUserGroup.GROUP_NM = this.txtGROUP_NM.Text.Trim();
            modUserGroup.MEMO = this.txtMEMO.Text.Trim();
            returnValue = bllUserGroup.AddOrUpdateUserGroup(modUserGroup);
            this.Cursor = Cursors.Default;
            if (returnValue.IsSuccess)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                Method.MessageShow(returnValue.ErrorID,dataName);
            }
        }               

        /// <summary>
        /// M002_USER_GROUP_KeyUp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M002_USER_GROUP_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            switch (e.KeyCode)
            {
                case Keys.F5:
                    btnAdd_Click(sender, e);
                    break;
                case Keys.F12:
                    btnClear_Click(sender, e);
                    break;
                case Keys.Escape:
                    btnExit_Click(sender, e);
                    break;
            }
        }

        #endregion
    }
}